#!/usr/bin/python
# encoding:utf-8

# ****************************************************************
# Description：邮件处理
# Author： jf
# Date：20170605
# version：
# ****************************************************************

import smtplib
from email.mime.text import MIMEText
from email.header import Header
import mod_config

# 第三方 SMTP 服务
mail_host = "smtp.qiye.163.com"  # 设置服务器
mail_user = mod_config.getConfig("CFG", "mail_user")  # 用户名
mail_password = mod_config.getConfig("CFG", "mail_password")  # 口令


def SendMail (From, To, Title, mail_msg):
	# @
	#    From：发件人
	#    To：收件人
	#    Title：邮件标题
	#    mail_msg：邮件内容（可以是html，或文本）
	To = To.split(",", 1)
	message = MIMEText(mail_msg, 'html', 'utf-8')
	message['From'] = Header(From, 'utf-8')
	message['To'] = ",".join(To)
	#     subject = 'Python SMTP 邮件测试'
	message['Subject'] = Header(Title, 'utf-8')

	try:
		smtpObj = smtplib.SMTP()
		smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
		smtpObj.login(mail_user, mail_password)
		smtpObj.sendmail(From, To, message.as_string())
		smtpObj.close()
		print u"邮件发送成功"
	except smtplib.SMTPException:
		smtpObj.close()
		print u"Error: 无法发送邮件"


if __name__ == '__main__':
	mail_msg = \
		"""
			<p>Python 邮件发送测试...</p>
			<p><a href="http://www.baidu.com">这是一个链接</a></p> 
		"""
	#     with open('sample.html', 'r') as newF:
	#         mail_msg = newF.read();
	#     newF.close();
	SendMail("jiangfei@longsung.com", "jiangfei@longsung.com", "我来测试一下发邮件的方法", mail_msg)
